﻿var SystemUser = {};
core.View.DoAfterView = function (id) {

    $(core.View.AddButton('btn_Changebusinessunit', "更改业务部门", "glyphicon-share-alt")).unbind().on('click', function () {
            var Checked = $("[name=ListContainer] input[type=checkbox][oid]:checked");           
            if (Checked.length <= 0) {
                alert("请选择");
                return;
            }           
            $(core.View.CreateLookupListModal()).on('click', function () {
                var SelectId='';
                $('div[id=LookupListContainer-body] table tbody tr').each(function () {
                    if ($(this).find('td:first').find('input').prop('checked')) {
                        SelectId = $(this).attr('oid');
                         return false;
                    }                    
                });
             //   $('#myModal').modal('hide');
                core.View.CreateModal("提示", "<div  style=\"text-align:center\"><p id=\"myModelContent\">  </p>\
                        <div id=\"progress\" class=\"progress \" style=\"opacity:0;height:0px\">\
                            <div  id=\"progress_bar\" class=\"progress-bar progress-bar-striped active  \" role=\"progressbar\" aria-valuemax=\"100\" aria-valuemin=\"0\" style=\"width: 0% ;\">\
                                <span class=\"sr-only\">60%</span>\
                            </div>\
                        </div></div>", "确定", "关闭");
                if (SelectId != '') {
                    SystemUser.ChangeBusinessUnitOrSystemuser(0, 0, SelectId);
                }
           });
         
            core.Lookup.BindLookupList("businessunit");
            $('#myModal').modal({ backdrop: 'static', keyboard: false });
        });



    $(core.View.AddButton('btn_MigratioDdata', "迁移数据", "glyphicon-share-alt")).unbind().on('click',
        function () {
            var Checked = $("[name=ListContainer] input[type=checkbox][oid]:checked");
            if (Checked.length <= 0) {
                alert("请选择");
                return;

            }
            $(core.View.CreateLookupListModal()).on('click',function () {
                var SelectId = '';
                $('div[id=LookupListContainer-body] table tbody tr').each(function () {
                    if ($(this).find('td:first').find('input').prop('checked')) {
                        SelectId = $(this).attr('oid');
                        return false;
                    }
                });
              //  $('#myModal').modal('hide');
                core.View.CreateModal("提示", "<div  style=\"text-align:center\"><p id=\"myModelContent\">  </p>\
                        <div id=\"progress\" class=\"progress \" style=\"opacity:0;height:0px\">\
                            <div  id=\"progress_bar\" class=\"progress-bar progress-bar-striped active  \" role=\"progressbar\" aria-valuemax=\"100\" aria-valuemin=\"0\" style=\"width: 0% ;\">\
                                <span class=\"sr-only\">60%</span>\
                            </div>\
                        </div></div>", "确定", "关闭");
                if (SelectId != '')
                {
                    //SystemUser.ChangeBusinessUnitOrSystemuser(0, 1, SelectId)
                    SystemUser.ChangeBusinessUnitOrSystemuser(0, 1, SelectId);

                }
                });
            core.Lookup.BindLookupList("systemuser");
            $('#myModal').modal({ backdrop: 'static', keyboard: false });
        });
}
SystemUser.ChangeBusinessUnitOrSystemuser = function (index, type, SelectId) {

    if (index == 0) {
        $("#myModal").modal({ backdrop: 'static', keyboard: false });
        list = $("[name=ListContainer] input[type=checkbox][oid]:checked");
    }
    var num = list.length;
    if (type == '0') {
        if (index < num) {
            core.showProgressBar(index, num, "更改");
            $.ajax({
                type: "post",
                async: true,
                url: "/Service/Privilege/ChangeBs.ashx",
                dataType: "json",
                data: {
                    "UserId": list.eq(index).attr("oid"),
                    "NewId": SelectId
                },
                success: function (data, textStatus) {
                    if (data.ResultCode == 0) {
                        SystemUser.ChangeBusinessUnitOrSystemuser(index + 1, 0, SelectId);
                    } else {
             //           alert(JSON.stringify(data));
                        core.ShowProgressError(index, num, data, "更改");
                    }
                },
                error: function (data, textStatus) {

                    alert("ajax未知错误");
                }
            });
        }
        if (index == num) {
            core.showProgressBar(index, num, "更改");
            //setTimeout("Response()", 3000);
        }
    }
    if (type == '1') {
        if (index < num) {
            core.showProgressBar(index, num, "迁移");
            $.ajax({
                url: "/Service/Privilege/MigrateUserRecords.ashx",
                async: true,
                type: "post",
                dataType: "json",
                data: {
                    "SystemUserId": list.eq(index).attr("oid"),
                    "ToWho": SelectId
                },
                success: function (data, textStatus) {
                    if (data.ResultCode == 0) {
                        SystemUser.ChangeBusinessUnitOrSystemuser(index + 1, 1, SelectId);
                    } else {
                //        alert(JSON.stringify(data));
                        core.ShowProgressError(index, num, data, "迁移");
                    }
                   
                },
                error: function (data, textStatus) {

                    alert("ajax错误");
                }
            });
        }
        if (index == num) {
            core.showProgressBar(index, num, "迁移");
        }

    }
}